﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework
{
    class Program
    {
        static void Main(string[] args)
        {
            //查找集合中是否含有abc元素
            ArrayList myList = new ArrayList(2);
            string[] stringArray = { "aaa", "bbb", "ccc", "abc", "Even" };
            myList.AddRange(stringArray);
            int index =myList.BinarySearch("abc");
            if (index != -1)
            {
                Console.WriteLine("集合中存在abc元素！");
            }
            else
            {
                Console.WriteLine("集合中不存在abc元素！");
            }
            Console.WriteLine();


            //将集合中下标为偶数的元素添加到另一个集合中
            ArrayList otherList = new ArrayList();
            for (int i = 0; i < myList.Count; i = i + 2)
            {
                otherList.Add(myList[i]);
            }
            foreach (string even in otherList)
            {
                Console.WriteLine(even);
            }
            Console.WriteLine();


            //在集合中第一个元素的后面任意插入3个元素
            ArrayList insertList = new ArrayList() { "1","2","3"};
            myList.InsertRange(1, insertList);
            foreach (var insert in insertList)
            {
                Console.WriteLine(insert);
            }
            Console.WriteLine();


            //将集合中的元素使用Sort方法排序后输出
            myList.Sort();
            Console.WriteLine("使用Sort方法按字母顺序排序如下:");
            foreach(var element in myList)
            {
                Console.WriteLine(element);
            }
            Console.WriteLine();


            //使用ICompare的方式来排序输出string
            MyCompare myCompare = new MyCompare();
            myList.Sort(myCompare);
            Console.WriteLine("使用带自定义比较器的Sort方法排序如下:");
            foreach (var element in myList)
            {
                Console.WriteLine(element);
            }
        }
    }
}
